﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Web;
using Newtonsoft.Json;

namespace PrintLabel
{
    internal class HttpService
    {
        public void open(Main form)
        {
            this.form = form;
            HttpListener listener = new HttpListener();
            listener.Prefixes.Add("http://*:10101/");
            listener.Start();
            for (; ; )
            {
                HttpListenerContext ctx = listener.GetContext();
                try
                {
                    ThreadPool.QueueUserWorkItem(delegate (object _)
                    {
                        bool flag = ctx.Request.Url.Segments.Length == 2;
                        if (flag)
                        {
                            this.returnReulr(ctx, "请求格式错误");
                        }
                        else
                        {
                            bool flag2 = ctx.Request.Url.Segments.Length > 1;
                            if (flag2)
                            {
                                string methodName = ctx.Request.Url.Segments[1].Replace("/", "");
                            }
                            string[] strParams = (from s in ctx.Request.Url.Segments.Skip(2)
                                                  select s.Replace("/", "")).ToArray<string>();
                            Result result = ResultGenerator.fail("请求路径不存在");
                            Stream stream = ctx.Request.InputStream;
                            StreamReader reader = new StreamReader(stream, Encoding.UTF8);
                            string data = HttpUtility.UrlDecode(reader.ReadToEnd()).Replace("data=", "");
                            Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff") + "收到POST数据：\r\n" + data);
                            string text = strParams[0];
                            string a = text;
                            if (!(a == "print"))
                            {
                                if (!(a == "showPrint"))
                                {
                                    if (!(a == "getPrintNames"))
                                    {
                                        result = ResultGenerator.fail("接口不存在");
                                    }
                                    else
                                    {
                                        result = this.getPrintNames();
                                    }
                                }
                                else
                                {
                                    result = this.showPrint(data);
                                }
                            }
                            else
                            {
                                result = this.Print(data);
                            }
                            this.returnReulr(ctx, JsonConvert.SerializeObject(result));
                        }
                    });
                }
                catch (Exception e)
                {
                }
            }
        }

        // Token: 0x06000020 RID: 32 RVA: 0x00003CB8 File Offset: 0x00001EB8
        private Result getPrintNames()
        {
            List<string> printNames = this.form.GetLocalPrinters();
            return ResultGenerator.success(printNames);
        }

        // Token: 0x06000021 RID: 33 RVA: 0x00003CDC File Offset: 0x00001EDC
        private void returnReulr(HttpListenerContext ctx, string retstr)
        {
            try
            {
                using (StreamWriter writer = new StreamWriter(ctx.Response.OutputStream, Encoding.UTF8))
                {
                    Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff") + retstr);
                    writer.WriteLine(retstr);
                    writer.Close();
                    ctx.Response.Close();
                }
            }
            catch (Exception e)
            {
            }
        }

        // Token: 0x06000022 RID: 34 RVA: 0x00003D70 File Offset: 0x00001F70
        public Result Print(string data)
        {
            bool flag = data == null || data == "";
            Result result;
            if (flag)
            {
                result = ResultGenerator.fail("参数不能为空");
            }
            else
            {
                HttpData httpData;
                try
                {
                    httpData = JsonConvert.DeserializeObject<HttpData>(data);
                }
                catch (Exception e)
                {
                    return ResultGenerator.fail("参数格式错误1");
                }
                bool flag2 = httpData.printerName == null || httpData.printerName == "";
                if (flag2)
                {
                    result = ResultGenerator.fail("打印机名称不能为空");
                }
                else
                {
                    bool flag3 = httpData.data == null || httpData.data == "";
                    if (flag3)
                    {
                        result = ResultGenerator.fail("打印数据不能为空");
                    }
                    else
                    {
                        bool flag4 = httpData.templateAddress == null || httpData.templateAddress == "";
                        if (flag4)
                        {
                            result = ResultGenerator.fail("打印模板地址不能为空");
                        }
                        else
                        {
                            List<Dictionary<string, string>> keyvalues = new List<Dictionary<string, string>>();
                            try
                            {
                                keyvalues = JsonConvert.DeserializeObject<List<Dictionary<string, string>>>(httpData.data);
                            }
                            catch (Exception e2)
                            {
                                return ResultGenerator.fail("打印数据格式错误2");
                            }
                            bool flag5 = keyvalues.Count == 0;
                            if (flag5)
                            {
                                result = ResultGenerator.fail("打印数据不能为空");
                            }
                            else
                            {
                                string message;
                                bool printStatus = this.form.Print2(keyvalues, httpData.templateAddress, out message, httpData.printerName, httpData.printNum);
                                bool flag6 = printStatus;
                                if (flag6)
                                {
                                    result = ResultGenerator.success(message);
                                }
                                else
                                {
                                    result = ResultGenerator.fail(message);
                                }
                            }
                        }
                    }
                }
            }
            return result;
        }

        // Token: 0x06000023 RID: 35 RVA: 0x00003EFC File Offset: 0x000020FC
        public Result showPrint(string data)
        {
            bool flag = data == null || data == "";
            Result result;
            if (flag)
            {
                result = ResultGenerator.fail("参数不能为空");
            }
            else
            {
                HttpData httpData;
                try
                {
                    httpData = JsonConvert.DeserializeObject<HttpData>(data);
                }
                catch (Exception e)
                {
                    return ResultGenerator.fail("参数格式错误1");
                }
                bool flag2 = httpData.data == null || httpData.data == "";
                if (flag2)
                {
                    result = ResultGenerator.fail("预览数据不能为空");
                }
                else
                {
                    bool flag3 = httpData.templateAddress == null || httpData.templateAddress == "";
                    if (flag3)
                    {
                        result = ResultGenerator.fail("预览模板地址不能为空");
                    }
                    else
                    {
                        List<Dictionary<string, string>> keyvalues;
                        try
                        {
                            keyvalues = JsonConvert.DeserializeObject<List<Dictionary<string, string>>>(httpData.data);
                        }
                        catch (Exception e2)
                        {
                            return ResultGenerator.fail("预览数据格式错误2");
                        }
                        bool flag4 = keyvalues.Count == 0;
                        if (flag4)
                        {
                            result = ResultGenerator.fail("预览数据不能为空");
                        }
                        else
                        {
                            string message;
                            bool printStatus = this.form.printShow(keyvalues, httpData.templateAddress, out message);
                            bool flag5 = printStatus;
                            if (flag5)
                            {
                                result = ResultGenerator.success(message);
                            }
                            else
                            {
                                result = ResultGenerator.fail(message);
                            }
                        }
                    }
                }
            }
            return result;
        }

        // Token: 0x04000024 RID: 36
        private Main form = null;
    }
}
